package com.mes.controller;

import com.mes.utils.HttpConnectionData;
import com.mes.utils.StringConvertUtils;
import com.yy.base.page.Message;
import com.yy.base.util.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;

/**
 * 获取Rfid数据
 */
@Controller
@RequestMapping(value="/rfid")
public class RfidController {
	/**
	 * 获取平板、PDA读取到的RFID信息
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/getAndroidRfid")
	public Message getAndroidRfid(HttpServletRequest request) {
		Message msg = new Message();
		try {
			String ip = StringUtil.getClientIp(request);
			String rfid = HttpConnectionData.sendGet("http://" + ip + ":28088/xdRfid/read", null);
			msg.setData("data", rfid);
		} catch (Exception e) {
			msg.setErrorMessage("获取RFID异常：" + e.getMessage());
		}
		return msg;
	}

	@ResponseBody
	@RequestMapping(value="/toString")
	public Message toString(HttpServletRequest request) {
		Message msg = new Message();
		try {
			String value = request.getParameter("value");
			//将RFID内容转换为字符
			value = StringConvertUtils.hexStringToAscii(value);
			value = StringConvertUtils.asciiToString(value);
			msg.setData("value", value);
		} catch (Exception e) {
			msg.setErrorMessage("RFID转换异常：" + e.getMessage());
		}
		return msg;
	}

	@ResponseBody
	@RequestMapping(value="/getRfid")
	public Message getRfid(HttpServletRequest request) {
		Message msg = new Message();
		try {
			String ip = StringUtil.getClientIp(request);
			msg.setData("data", HttpConnectionData.sendGet("http://" + ip + ":8080/getrfid", null));
		} catch (Exception e) {
			msg.setErrorMessage("获取RFID异常：" + e.getMessage());
		}
		return msg;
	}

	/**
	 * 写入RFID
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/writeRfid")
	public Message writeRfid(HttpServletRequest request) {
		Message msg = new Message();
		try {
			String code = request.getParameter("code").trim();
			String ip = StringUtil.getClientIp(request);
			String result = HttpConnectionData.sendGet("http://" + ip + ":28088/xdRfid/write?rfid=" + code, null);
			msg.setData("data", result);
			msg.setOkMessage("操作成功");
		} catch (Exception e) {
			msg.setErrorMessage("获取RFID异常：" + e.getMessage());
		}
		return msg;
	}
}